package com.yoho.vpm

import android.support.v4.app.Fragment
import android.support.v4.app.FragmentPagerAdapter
import android.view.View
import com.yoho.vpm.base.BaseActivity
import com.yoho.vpm.module.comic.ComicFragment
import com.yoho.vpm.module.live.LiveFragment
import com.yoho.vpm.module.music.MusicFragment
import com.yoho.vpm.module.news.NewsFragment
import com.yoho.vpm.module.story.StoryFragment
import kotlinx.android.synthetic.main.activity_main.*
import java.util.*

class HomeActivity : BaseActivity(), View.OnClickListener {
    override fun onClick(v: View) {
        when (v.id) {
            R.id.bbv_music -> {
                vp_home.currentItem = 0
                operateClickState(false, true, true, true, true)
            }
            R.id.bbv_news -> vp_home.currentItem = 1
            R.id.bbv_comic -> vp_home.currentItem = 2
            R.id.bbv_live -> vp_home.currentItem = 3
            R.id.bbv_story -> vp_home.currentItem = 4
        }
    }

    override val viewId: Int = R.layout.activity_main

    override fun init() {
        initViewPager()
        bbv_music.setOnClickListener(this)
        bbv_news.setOnClickListener(this)
        bbv_comic.setOnClickListener(this)
        bbv_live.setOnClickListener(this)
        bbv_story.setOnClickListener(this)
    }


    private fun initViewPager() {
        mFragments.add(MusicFragment())
        mFragments.add(NewsFragment())
        mFragments.add(ComicFragment())
        mFragments.add(LiveFragment())
        mFragments.add(StoryFragment())
        vp_home.offscreenPageLimit = 5
        vp_home.adapter = mAdapter
    }

    private val mFragments = ArrayList<Fragment>(5)

    private val mAdapter = object : FragmentPagerAdapter(supportFragmentManager) {
        override fun getItem(position: Int): Fragment = mFragments[position]
        override fun getCount(): Int = mFragments.size
    }

    private fun operateClickState(music: Boolean, news: Boolean, comic: Boolean, live: Boolean, story: Boolean) {
        bbv_music.isClickable = music
        bbv_news.isClickable = news
        bbv_comic.isClickable = comic
        bbv_live.isClickable = live
        bbv_story.isClickable = story
    }
}
